package com.settlement.system.model.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.settlement.system.common.base.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 京东账单
 * </p>
 *
 * @author huangrensen
 * @since 2024-05-17
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class JdBill extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @TableId(value = "id")
    private String id;


    /**
     * 店铺ID
     */
    private Long storeId;
    @TableField(exist = false)
    private String storeName;


    /**
     * 订单编号
     */
    private String orderNo;
    // 批量查询
    @TableField(exist = false)
    private List<String> orderNos;


    /**
     * 单据编号
     */
    private String documentNo;


    /**
     * 单据类型
     */
    private String documentType;


    /**
     * 商品编号
     */
    private String productCode;


    /**
     * 商户订单号
     */
    private String sellerNo;


    /**
     * 商品名称
     */
    private String productName;


    /**
     * 费用发生时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime feeHappenTime;


    /**
     * 费用计算时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime feeCalculateTime;


    /**
     * 费用结算时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime feeSettlementTime;


    /**
     * 费用项
     */
    private String feeProject;


    /**
     * 金额
     */
    private BigDecimal feeAmount;


    /**
     * 币种
     */
    private String currency;


    /**
     * 商家应收应付
     */
    private String sellerReceivePay;


    /**
     * 钱包结算备注
     */
    private String settlementRemark;


    /**
     * 店铺号
     */
    private String storeNo;


    /**
     * 京东门店编号
     */
    private String jdStoreNo;


    /**
     * 品牌门店编号
     */
    private String brandStoreNo;


    /**
     * 门店名称
     */
    private String shopName;


    /**
     * 备注
     */
    private String remark;


    /**
     * 收支方向
     */
    private String direction;


    /**
     * 账单日期
     */
    private String billDate;


    /**
     * 商品数量
     */
    private Integer qty;

    @TableField(exist = false)
    private String tableName;
    @TableField(exist = false)
    private String billMonth;

    private static boolean compareDate(LocalDateTime date1, LocalDateTime date2) {
        if (date1 == null && date2 == null) {
            return true;
        }
        if (date1 != null && date2 == null) {
            return false;
        }
        return date1 != null && date1.equals(date2);
    }

    private static boolean compareBigDecimal(BigDecimal value1, BigDecimal value2) {
        if (value1 == null && value2 == null) {
            return true;
        }
        if (Objects.isNull(value2)) {
            value2 = BigDecimal.ZERO;
        }
        return value1 != null && value1.compareTo(value2) == 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        JdBill jdBill = (JdBill) o;
        return Objects.equals(storeId, jdBill.storeId) && Objects.equals(orderNo, jdBill.orderNo) && Objects.equals(documentNo, jdBill.documentNo) && Objects.equals(documentType, jdBill.documentType) && Objects.equals(productCode, jdBill.productCode) && Objects.equals(sellerNo, jdBill.sellerNo) && Objects.equals(productName, jdBill.productName) && compareDate(feeHappenTime, jdBill.feeHappenTime) && compareDate(feeCalculateTime, jdBill.feeCalculateTime) && compareDate(feeSettlementTime, jdBill.feeSettlementTime) && Objects.equals(feeProject, jdBill.feeProject) && compareBigDecimal(feeAmount, jdBill.feeAmount) && Objects.equals(currency, jdBill.currency) && Objects.equals(sellerReceivePay, jdBill.sellerReceivePay) && Objects.equals(settlementRemark, jdBill.settlementRemark) && Objects.equals(storeNo, jdBill.storeNo) && Objects.equals(jdStoreNo, jdBill.jdStoreNo) && Objects.equals(brandStoreNo, jdBill.brandStoreNo) && Objects.equals(shopName, jdBill.shopName) && Objects.equals(remark, jdBill.remark) && Objects.equals(direction, jdBill.direction) && Objects.equals(billDate, jdBill.billDate) && Objects.equals(qty, jdBill.qty);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), storeId, orderNo, documentNo, documentType, productCode, sellerNo, productName, feeHappenTime, feeCalculateTime, feeSettlementTime, feeProject, feeAmount, currency, sellerReceivePay, settlementRemark, storeNo, jdStoreNo, brandStoreNo, shopName, remark, direction, billDate, qty);
    }

    // 截取长度超出范围的字段
    public void cutString() {
        if (this.productName != null && this.productName.length() > 100) {
            this.productName = this.productName.substring(0, 100);
        }
        if (this.feeProject != null && this.feeProject.length() > 50) {
            this.feeProject = this.feeProject.substring(0, 50);
        }
        if (this.settlementRemark != null && this.settlementRemark.length() > 2000) {
            this.settlementRemark = this.settlementRemark.substring(0, 2000);
        }
        if (this.remark != null && this.remark.length() > 2000) {
            this.remark = this.remark.substring(0, 2000);
        }
    }
}
